package cn.itcast.servlet;

import cn.itcast.entity.FoodType;
import cn.itcast.exception.NullException;
import cn.itcast.factory.BeanFactory;
import cn.itcast.service.FoodTypeService;
import cn.itcast.utils.WebUtils;
import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FoodTypeServlet extends HttpServlet {
	private FoodTypeService foodTypeService = (FoodTypeService) BeanFactory
			.getInstance("FoodTypeServiceImpl", FoodTypeService.class);
	private String uri;

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");

		String method = request.getParameter("method");

		if ("list".equals(method)) {
			list(request, response);
		} else if ("add".equals(method)) {
			add(request, response);
		} else if ("delete".equals(method)) {
			delete(request, response);
		} else if ("viewUpdate".equals(method)) {
			viewUpdate(request, response);
		} else if ("update".equals(method)) {
			update(request, response);
		} else if ("search".equals(method)) {
			search(request, response);
		}
	}

	private void search(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			String typeName = request.getParameter("typeName");

			List list = this.foodTypeService.getAll(typeName);

			request.setAttribute("foodTypeList", list);

			this.uri = "/sys/type/cuisineList.jsp";
		} catch (NullException e) {
			request.setAttribute("msg", e.getMessage());

			this.uri = "/sys/type/cuisineList.jsp";
		} catch (Exception e) {
			e.printStackTrace();

			this.uri = "/error/error.jsp";
		}

		request.getRequestDispatcher(this.uri).forward(request, response);
	}

	private void update(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		try {
			FoodType ft = (FoodType) WebUtils.copeToBean(request,
					FoodType.class);

			this.foodTypeService.update(ft);

			this.uri = "/FoodTypeServlet?method=list";
		} catch (Exception e) {
			e.printStackTrace();

			this.uri = "/error/error.jsp";
		}

		response.sendRedirect(request.getContextPath() + this.uri);
	}

	private void viewUpdate(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			String id = request.getParameter("id");

			FoodType ft = this.foodTypeService.findById(Integer.parseInt(id));

			request.setAttribute("foodType", ft);

			this.uri = "/sys/type/updateCuisine.jsp";
		} catch (Exception e) {
			e.printStackTrace();

			this.uri = "/error/error.jsp";
		}

		request.getRequestDispatcher(this.uri).forward(request, response);
	}

	private void delete(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		try {
			String id = request.getParameter("id");

			this.foodTypeService.delete(Integer.parseInt(id));

			this.uri = "/FoodTypeServlet?method=list";
		} catch (Exception e) {
			e.printStackTrace();

			this.uri = "/error/error.jsp";
		}

		response.sendRedirect(request.getContextPath() + this.uri);
	}

	private void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			FoodType ft = (FoodType) WebUtils.copeToBean(request,
					FoodType.class);

			this.foodTypeService.add(ft);

			this.uri = "/FoodTypeServlet?method=list";
		} catch (Exception e) {
			e.printStackTrace();

			this.uri = "/error/error.jsp";
		}

		request.getRequestDispatcher(this.uri).forward(request, response);
	}

	private void list(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			List list = this.foodTypeService.getAll();

			request.setAttribute("foodTypeList", list);

			this.uri = "/sys/type/cuisineList.jsp";
		} catch (Exception e) {
			e.printStackTrace();

			this.uri = "/error/error.jsp";
		}

		request.getRequestDispatcher(this.uri).forward(request, response);
	}
}